import logging
import sqlite3

##############
#author:张子路#
##############

class CreatStandrand():

    def __init__(self):
        try:
            self.conn = sqlite3.connect("./DEMO.db")
            self.cur = self.conn.cursor()
        except Exception as e:
            logging.error("数据库读取创建出错 {0}".format(e), exc_info=True)
        try:
            self.cur.execute('DROP TABLE IF EXISTS StandrandConfigation')        #删除原表单
        except Exception as e:
            logging.error("数据库原表单删除出错 {0}".format(e), exc_info=True)
        try:      #创建安全策略标准表单
            sql='CREATE TABLE IF NOT EXISTS StandrandConfigation(id INTEGER PRIMARY KEY, ' \
                'eval_para TEXT,' \
                'cmpr_method TEXT,' \
                'cmpr_val TEXT,' \
                'hrdn_val TEXT' \
                ')'
            self.cur.execute(sql)
        except Exception as e:
            logging.error("数据库表单创建出错 {0}".format(e), exc_info=True)

    def CloseSql(self):
        try:       #关闭数据库
            self.conn.close()
        except Exception:
            print('关闭数据库出错。')

    def Insert(self):       #插入数据
        date=[[None,str(('System Access','MaximumPasswordAge')),str(['st','not equ']),str(['60','0']),'60'],
              [None,str(('System Access','PasswordHistorySize')),str(['bt']),str(['24']),'24'],
              [None,str(('System Access','PasswordComplexity')),str(['equ']),str(['1']),'1'],
              [None,str(('System Access','ResetLockoutCount')),str(['bt']),str(['15']),'15'],
              [None,str(('System Access','LockoutBadCount')),str(['st','not equ']),str(['10','0']),'10'],
              [None,str(('Privilege Rights','SeNetworkLogonRight')),str(['set to']),str([['*S-1-5-32-544','*S-1-5-11','*S-1-5-9']]),'*S-1-5-32-544,*S-1-5-11,*S-1-5-9'],
              [None,str(('Privilege Rights','SeBackupPrivilege')),str(['set to']),str([['*S-1-5-32-544']]),'*S-1-5-32-544'],
              [None,str(('Privilege Rights','SeCreateSymbolicLinkPrivilege')),str(['set to']),str([['*S-1-5-32-544']]),'*S-1-5-32-544'],
              [None,str(('Privilege Rights','SeProfileSingleProcessPrivilege')),str(['set to']),str([['*S-1-5-32-544']]),'*S-1-5-32-544'],
              [None,str(('Privilege Rights','SeSystemProfilePrivilege')),str(['set to']),str([['*S-1-5-32-544','*S-1-5-80']]),'*S-1-5-32-544,*S-1-5-80']
              ]
        try:
            self.conn.executemany('INSERT INTO StandrandConfigation VALUES (?,?,?,?,?)',date)
            self.conn.commit()       #提交数据
        except Exception as e:
            logging.error("数据提交出错 {0}".format(e), exc_info=True)

    def FindAll(self):
        try:       #查询数据库中的值
            self.cur.execute('select * from StandrandConfigation')
            for iteam in self.cur.fetchall():
                print(iteam)
        except Exception as e:
            logging.error("数据查询出错 {0}".format(e), exc_info=True)

if __name__ =='__main__':
    si=CreatStandrand()
    si.Insert()
    si.FindAll()
    si.CloseSql()